<template>
  <div class="coffee_machine_nearby">
    <c-title :hide="false" text="附近设备"></c-title>
    <div class="title" @click="toLaction">
      <div class="txt">{{ address || "全国" }}</div>
      <i class="iconfont icon-icon_more"></i>
    </div>
    <div class="list" v-for="(item, index) in info" :key="index">
      <div class="left">
        <div class="left_top">
          <div class="left_top_name">设备名称:{{ item.name }}</div>
          <div class="left_top_icon" @click="goToAdress(item)">
            <i class="iconfont icon-icon_publish"></i>
            {{ item.distance }}
          </div>
        </div>
        <div class="left_bottom">设备地址: {{ setAddress(item) }}</div>
      </div>
      <div class="right" @click="toSearch(item.id)">选择商品</div>
    </div>
  </div>
</template>
<script>
import { scrollMixin } from "utils/mixin";
export default {
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      city: "",
      address: "",
      point: "",
      info: [],

      isLoadMore: true,
      page: 1,
      total_page: 0
    };
  },
  computed: {
    setAddress(item) {
      return function(item) {
        return `${item.has_one_province.areaname}${item.has_one_city.areaname}${item.has_one_district.areaname}${item.address}`;
      };
    }
  },
  activated() {
    let myLocation = JSON.parse(localStorage.getItem("myLocation"));
    if (myLocation && myLocation.timestamp) {
      //解决定位选择“全国”后返回或者跳转其他页面返回又重新定位当前
      this.city = myLocation.city;
      this.address = myLocation.title;
      this.point = myLocation.point;
      this.getMachineList();
    } else {
      this.fun
        .getLocation("正在定位")
        .then(res => {
          this.city = res.city;
          this.address = res.title;
          this.point = res.point;
          this.getMachineList();
        })
        .catch(err => {
          console.log(err);
        });
    }
  },
  methods: {
    toLaction() {
      this.$router.push(this.fun.getUrl("coffee_machine_location", {}, { tag: "coffee" }));
    },
    getMachineList() {
      let json = {
        lng: this.point.lng,
        lat: this.point.lat,
        city_name: this.city
      };
      $http
        .get("plugin.coffee-machine.frontend.buyer.machineList", json)
        .then(response => {
          if (response.result == 1) {
            console.log(response.data.data, "response");
            this.info = response.data.data;
            this.total_page = response.data.last_page;
            this.page = response.data.current_page;
          } else {
            this.$toast(response.msg);
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    toSearch(id) {
      this.$router.push(
        this.fun.getUrl("coffee_machine_cate", {
          machine_id: id
        })
      );
    },
    goToAdress(item) {
      let point = this.fun.bd_decrypt(item.lng, item.lat);
      this.fun.goToWXAdress(point, item.name, item.address);
    }
  }
};
</script>
<style lang="scss" scoped>
.coffee_machine_nearby {
  padding: 0.75rem;
  box-sizing: border-box;
  .title {
    text-align: left;
    font-size: 0.94rem;
    font-weight: bold;
    display: flex;
    align-items: center;
    .txt {
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    .icon-icon_more {
      font-size: 0.375rem;
      margin-left: 0.25rem;
    }
  }
}
.list {
  background: #fff;
  box-sizing: border-box;
  border-radius: 0.75rem;
  margin-top: 0.5rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem;
  .left {
    flex: 1;
    overflow: hidden;
    .left_bottom {
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: flex-start;
      overflow: hidden;
      margin-top: 0.75rem;
      font-size: 0.75rem;
      color: #999999;
    }
    .left_top {
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: flex-start;
      overflow: hidden;
      .left_top_name {
        flex: 1;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        color: #333333;
        font-size: 0.88rem;
        text-align: left;
      }
      .left_top_icon {
        margin-left: 0.75rem;
        flex-shrink: 0;
        color: #28b2fa;
        font-size: 0.69rem;
        .iconfont {
          font-size: 0.69rem;
        }
      }
    }
  }
  .right {
    margin-left: 0.5rem;
    flex-shrink: 0;
    width: 4.69rem;
    height: 1.5rem;
    background: #28b2fa;
    border-radius: 1.09rem;
    text-align: center;
    line-height: 1.5rem;
    color: #fff;
  }
}
</style>
